home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Xconq 7.1.0 / src / xconq-7.1.0 / kernel / unit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-07  |  56.5 KB  |  2,260 lines  |  [TEXT/R*ch]

  1. /* Units in Xconq.
  2.    Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996
  3.    Stanley T. Shebs.
  4.  
  5. Xconq is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.  See the file COPYING.  */
  9.  
  10. #include "conq.h"
  11. extern int change_cell PARAMS ((Unit *unit, int x, int y));
  12.  
  13. extern void set_unit_position PARAMS ((Unit *unit, int x, int y, int z));
  14. extern void add_unit_to_stack PARAMS ((Unit *unit, int x, int y));
  15. extern void remove_unit_from_stack PARAMS ((Unit *unit));
  16. extern void glimpse_adjacent_terrain PARAMS ((Unit *unit));
  17.  
  18. /* This is not a limit, just sets initial allocation of unit objects
  19.    and how many additional to get if more are needed.  This can be
  20.    tweaked for more frequent but smaller allocation, or less frequent
  21.    but larger and possibly space-wasting allocation. */
  22.  
  23. #ifndef INITMAXUNITS
  24. #define INITMAXUNITS 200
  25. #endif
  26.  
  27. static void allocate_unit_block PARAMS ((void));
  28. static int compare_units PARAMS ((Unit *unit1, Unit *unit2));
  29. static int compare_units_by_keys PARAMS ((const void *e1, const void *e2));
  30.  
  31. static void change_cell_aux PARAMS ((Unit *unit, int x0, int y0, int x, int y));
  32.  
  33. /* The list of available units. */
  34.  
  35. Unit *freeunits;
  36.  
  37. /* The global linked list of all units. */
  38.  
  39. Unit *unitlist;
  40.  
  41. /* A scratch global. */
  42.  
  43. Unit *tmpunit;
  44.  
  45. /* Buffers for descriptions of units. */
  46.  
  47. /* We use several and rotate among them; this is so multiple calls in
  48.    a single printf will work as expected.  Not elegant, but simple and
  49.    sufficient. */
  50.  
  51. #define NUMSHORTBUFS 3
  52.  
  53. int curshortbuf;
  54.  
  55. char *shortbufs[NUMSHORTBUFS] = { NULL, NULL, NULL };
  56.  
  57. char utypenamen[100];
  58.  
  59. char *actorstatebuf = NULL;
  60.  
  61. /* Total number of units in existence. */
  62.  
  63. int numunits;
  64.  
  65. /* Total number of units that are alive. */
  66.  
  67. int numliveunits;
  68.  
  69. /* Total number of live units, by type. */
  70.  
  71. int *numlivebytype;
  72.  
  73. /* The next number to use for a unit id. */
  74.  
  75. int nextid = 1;
  76.  
  77. /* A cache of completenesses. */
  78.  
  79. short *completenesses;
  80.  
  81. /* Have units died since dead unit lists made. */
  82.  
  83. int recent_dead_flushed = TRUE;
  84.  
  85. /* Local tmp var. */
  86.  
  87. static Side *tmpside_classtest;
  88.  
  89. /* Grab a block of unit objects to work with. */
  90.  
  91. static void
  92. allocate_unit_block()
  93. {
  94.     int i;
  95.     Unit *unitblock = (Unit *) xmalloc(INITMAXUNITS * sizeof(Unit));
  96.  
  97.     for (i = 0; i < INITMAXUNITS; ++i) {
  98.         unitblock[i].id = -1;
  99.         unitblock[i].next = &unitblock[i+1];
  100.     }
  101.     unitblock[INITMAXUNITS-1].next = NULL;
  102.     freeunits = unitblock;
  103.     Dprintf("Allocated space for %d units.\n", INITMAXUNITS);
  104. }
  105.  
  106. /* Init gets a first block of units, and sets up the "independent side" list,
  107.    since newly created units will appear on it. */
  108.  
  109. void
  110. init_units()
  111. {
  112.     unitlist = NULL;
  113.     allocate_unit_block();
  114.     init_side_unithead(indepside);
  115.     completenesses = (short *) xmalloc(MAXUTYPES * sizeof(short));
  116. }
  117.  
  118. /* The primitive unit creator, called by regular creator and also used to
  119.    make the dummy units that sit at the heads of the per-side unit lists. */
  120.  
  121. Unit *
  122. create_bare_unit(type)
  123. int type;
  124. {
  125.     Unit *newunit;
  126.  
  127.     /* If our free list is empty, go and get some more units. */
  128.     if (freeunits == NULL) {
  129.     allocate_unit_block();
  130.     }
  131.     /* Take the first unit off the free list. */
  132.     newunit = freeunits;
  133.     freeunits = freeunits->next;
  134.     /* Give it a valid type... */
  135.     newunit->type = type;
  136.     /* ...but an invalid id. */
  137.     newunit->id = -1;
  138.     return newunit;
  139. }
  140.  
  141. /* The regular unit creation routine.  All the slots should have something
  142.    reasonable in them, since individual units objects see a lot of reuse. */
  143.  
  144. Unit *
  145. create_unit(type, makebrains)
  146. int type, makebrains;
  147. {
  148.     int m;
  149.     Unit *newunit;
  150.  
  151.     if (numlivebytype == NULL) {
  152.         numlivebytype = (int *) xmalloc(MAXUTYPES * sizeof(int));
  153.     }
  154.     /* Fill this in. */
  155.     if (completenesses[0] == 0) {
  156.     int i;
  157.     for_all_unit_types(i)
  158.       completenesses[i] = u_cp(i) / u_parts(i);
  159.     }
  160.     /* Test whether we've hit any designer-specified limits. */
  161.     if ((u_type_in_game_max(type) >= 0
  162.      && numlivebytype[type] >= u_type_in_game_max(type))
  163.     || (g_units_in_game_max() >= 0
  164.         && numliveunits >= g_units_in_game_max())) {
  165.     return NULL;
  166.     }
  167.     /* Allocate the unit object.  Xconq will fail instead of returning null. */
  168.     newunit = create_bare_unit(type);
  169.     /* Init all the slots to distinguishable values.  The unit is not
  170.        necessarily newly allocated, so we have to hit all of its slots. */
  171.     newunit->id = nextid++;
  172.     newunit->name = NULL;
  173.     /* Number == 0 means unit is unnumbered. */
  174.     newunit->number = 0;
  175.     /* Outside the world. */
  176.     newunit->x = newunit->y = -1;
  177.     /* At ground level. */
  178.     newunit->z = 0;
  179.     /* Units default to being independent. */
  180.     newunit->side = NULL;
  181.     newunit->origside = NULL;
  182.     /* Create at max hp, let others reduce if necessary. */
  183.     newunit->hp = newunit->hp2 = u_hp(type);
  184.     /* Create fully functional, let other routines set incompleteness. */
  185.     newunit->cp = u_cp(type);
  186.     /* Not in a transport. */
  187.     newunit->transport = NULL;
  188.     /* Note that the space never needs to be freed. */
  189.     if (newunit->supply == NULL && nummtypes > 0) {
  190.         newunit->supply = (short *) xmalloc(nummtypes * sizeof(short));
  191.     }
  192.     /* Always zero out all the supply values. */
  193.     for_all_material_types(m)
  194.       newunit->supply[m] = 0;
  195.     /* Will allocate tooling state when actually needed. */
  196.     newunit->tooling = NULL;
  197.     /* Will allocate opinions when actually needed. */
  198.     newunit->opinions = NULL;
  199.     if (makebrains) {
  200.     init_unit_actorstate(newunit, TRUE);
  201.     init_unit_plan(newunit);
  202.     } else {
  203.     newunit->act = NULL;
  204.     newunit->plan = NULL;
  205.     }
  206.     if (newunit->extras != NULL)
  207.       init_unit_extras(newunit);
  208.     newunit->occupant = NULL;
  209.     newunit->nexthere = NULL;
  210.     /* Glue this unit into the list of independent units. */
  211.     newunit->next = newunit;
  212.     newunit->prev = newunit;
  213.     insert_unit(indepside->unithead, newunit);
  214.     newunit->unext = unitlist;
  215.     unitlist = newunit;
  216.     /* Init more random slots. */
  217.     newunit->prevx = newunit->prevy = -1;
  218.     newunit->aihook = NULL;
  219.     /* Add to the global unit counts. */
  220.     ++numunits;
  221.     ++numliveunits;
  222.     ++(numlivebytype[type]);
  223.     return newunit;
  224. }
  225.  
  226. void
  227. init_unit_tooling(unit)
  228. Unit *unit;
  229. {
  230.     unit->tooling = (short *) xmalloc(numutypes * sizeof(short));
  231. }
  232.  
  233. void
  234. init_unit_opinions(unit, nsides)
  235. Unit *unit;
  236. int nsides;
  237. {
  238.     int i;
  239.     short *temp;
  240.  
  241.     temp = NULL;
  242.     if (u_has_opinions(unit->type)) {
  243.     if (unit->opinions != NULL && nsides > numsides) {
  244.         temp = unit->opinions;
  245.         unit->opinions = NULL;
  246.     }
  247.     if (unit->opinions == NULL)
  248.       unit->opinions = (short *) xmalloc(nsides * sizeof(short));
  249.     /* Opinions are now all neutral. */
  250.     if (temp != NULL) {
  251.         /* Copy over old opinions. */
  252.         for (i = 0; i < numsides; ++i)
  253.           unit->opinions[i] = temp[i];
  254.         free(temp);
  255.     }
  256.     } else {
  257.     if (unit->opinions != NULL)
  258.       free(unit->opinions);
  259.     unit->opinions = NULL;
  260.     }
  261. }
  262.  
  263. /* Alter the actorstate object to be correct for this unit. */
  264.  
  265. void
  266. init_unit_actorstate(unit, flagacp)
  267. Unit *unit;
  268. int flagacp;
  269. {
  270.     if (u_acp(unit->type) > 0 && unit->cp > 0) {
  271.     /* Might already have an actorstate, don't realloc if so;
  272.        but do clear an existing actorstate, since might be reusing. */
  273.     if (unit->act == NULL)
  274.       unit->act = (ActorState *) xmalloc(sizeof(ActorState));
  275.     else
  276.       memset((char *) unit->act, 0, sizeof(ActorState));
  277.     /* Indicate that the action points have not been set. */
  278.     if (flagacp)
  279.       unit->act->acp = u_acp_min(unit->type) - 1;
  280.     /* Flag the action as undefined. */
  281.     unit->act->nextaction.type = ACTION_NONE;
  282.     } else {
  283.     if (unit->act != NULL)
  284.       free(unit->act);
  285.     unit->act = NULL;
  286.     }
  287. }
  288.  
  289. void
  290. init_unit_extras(unit)
  291. Unit *unit;
  292. {
  293.     if (unit->extras == NULL) {
  294.     unit->extras = (UnitExtras *) xmalloc(sizeof(UnitExtras));
  295.     }
  296.     unit->extras->point_value = -1;
  297.     unit->extras->appear = -1;
  298.     unit->extras->appearx = -1;
  299.     unit->extras->appeary = -1;
  300.     unit->extras->disappear = -1;
  301.     unit->extras->hook = lispnil;
  302. }
  303.  
  304. /* Changing a unit's type has many effects. */
  305.  
  306. void
  307. change_unit_type(unit, newtype, reason)
  308. Unit *unit;
  309. int newtype, reason;
  310. {
  311.     int oldtype = unit->type, oldhp = unit->hp;
  312.     PastUnit *pastunit;
  313.     Side *oldside;
  314.  
  315.     /* Don't do anything if we're "changing" to the same type. */
  316.     if (oldtype == newtype)
  317.       return;
  318.     oldside = unit->side;
  319.     pastunit = change_unit_to_past_unit(unit);
  320.     if (reason >= 0)
  321.       record_event(reason, ALLSIDES, pastunit->id, unit->id);
  322.     /* Decrement viewing coverage of our old type. */
  323.     if (completed(unit)
  324.         && (unit->transport == NULL
  325.        || uu_occ_can_see(unit->type, unit->transport->type))) {
  326.     cover_area(unit->side, unit, unit->x, unit->y, -1, -1);
  327.     }
  328.     /* Do the actual change. */
  329.     unit->type = newtype;
  330.     /* Set the new hp to the same ratio of max as the unit had before.
  331.        Caller can tweak to something else if necessary. */
  332.     unit->hp = (oldhp * u_hp_max(newtype)) / u_hp_max(oldtype);
  333.     /* Need to guarantee a positive value though. */
  334.     if (unit->hp < 1)
  335.       unit->hp = 1;
  336.     unit->hp2 = unit->hp;
  337.     /* Invalidate the side's point value cache. */
  338.     if (unit->side)
  339.       unit->side->point_value_valid = FALSE;
  340.     /* We might have to change sides as a result. */
  341.     /* (should modify per-side counts) */
  342.     if (!type_allowed_on_side(newtype, unit->side)) {
  343.         if (type_allowed_on_side(newtype, NULL)) {
  344.         /* Unit becomes independent. */
  345.         change_unit_side(unit, NULL, reason, NULL);
  346.     } else {
  347.         run_warning("Leaving unit on disallowed side");
  348.     }
  349.     }
  350.     /* Unit will always need a new number. */
  351.     assign_unit_number(unit);
  352.     init_unit_opinions(unit, numsides);
  353.     /* This clears the unit's acp - desirable? */
  354.     init_unit_actorstate(unit, FALSE);
  355.     init_unit_plan(unit);
  356.     unit->aihook = NULL;
  357.     /* Increment viewing coverage, if unit is complete and can see out
  358.        of its transport. */
  359.     if (completed(unit)
  360.         && (unit->transport == NULL
  361.         || uu_occ_can_see(unit->type, unit->transport->type))) {
  362.     cover_area(unit->side, unit, -1, -1, unit->x, unit->y);
  363.     /* If vision range is 0, allow glimpses of adjacent cell terrain.
  364.        This applies to terrain only, adjacent units cannot be seen. */
  365.     if (u_vision_range(unit->type) == 0)
  366.       glimpse_adjacent_terrain(unit);
  367.     }
  368.     count_loss(oldside, oldtype, (reason == H_UNIT_WRECKED ? combat_loss : other_loss));
  369.     count_gain(unit->side, newtype, other_gain);
  370.     /* Update global counts. */
  371.     --(numlivebytype[oldtype]);
  372.     ++(numlivebytype[newtype]);
  373. }
  374.  
  375. int
  376. max_builds(u)
  377. int u;
  378. {
  379.     int u2;
  380.  
  381.     for_all_unit_types(u2) {
  382.     if (could_create(u, u2))
  383.       return 1;
  384.     }
  385.     return 0;
  386. }
  387.  
  388. /* A unit occupies a cell by adding itself to the list of occupants.
  389.    It will not occupy a transport even if one is at this position
  390.    (other code should have taken care of this case already)
  391.    If something goes wrong, return false.  This routine is heavily used. */
  392.  
  393. int
  394. enter_cell(unit, x, y)
  395. Unit *unit;
  396. int x, y;
  397. {
  398.     register int u = unit->type;
  399.     Unit *prevunit = NULL, *nextunit = NULL;
  400.  
  401. #ifdef DEBUGGING
  402.     /* Not necessarily an error, but indicative of bugs elsewhere. */
  403.     if (unit->x >= 0 || unit->y >= 0) {
  404.     run_warning("unit %d occupying cell (%d, %d), was at (%d %d)",
  405.             unit->id, x, y, unit->x, unit->y);
  406.     }
  407. #endif /* DEBUGGING */
  408.     /* Always check this one, but not necessarily fatal. */
  409.     if (!inside_area(x, y)) {
  410.     run_warning("No cell at %d,%d, %s can't enter it",
  411.             x, y, unit_desig(unit));
  412.     /* Let the unit remain off-world. */
  413.     return FALSE;
  414.     }
  415.     if (!can_occupy_cell(unit, x, y)) {
  416.     run_warning("Cell at %d,%d is too full for %s",
  417.             x, y, unit_desig(unit));
  418.     /* Let the unit remain off-world. */
  419.     return FALSE;
  420.     }
  421.     add_unit_to_stack(unit, x, y);
  422.     /* Set the location slots now. */
  423.     enter_cell_aux(unit, x, y);
  424.     /* Inevitable side-effect of appearing in the new location. */
  425.     all_see_occupy(unit, x, y, TRUE);
  426.     return TRUE;
  427. }
  428.  
  429. /* Return true if the given unit can fit onto the given cell. */
  430.  
  431. /* (should eventually account for variable-size units) */
  432.  
  433. int
  434. can_occupy_cell(unit, x, y)
  435. Unit *unit;
  436. int x, y;
  437. {
  438.     int u = unit->type, u2, u3, t = terrain_at(x, y), numthistype = 0;
  439.     int fullness = 0, tcap, utcap, numtypes[MAXUTYPES];
  440.     Unit *unit2;
  441.  
  442.     if (unit == NULL)
  443.       run_error("null unit?");  /* should never happen */
  444.     tcap = t_capacity(t);
  445.     utcap = ut_capacity_x(u, t);
  446.     if (tcap <= 0 && utcap <= 0)
  447.       return FALSE;
  448.     for_all_unit_types(u3)
  449.       numtypes[u3] = 0;
  450.     for_all_stack(x, y, unit2) {
  451.     u2 = unit2->type;
  452.     ++numtypes[u2];
  453.     if (u2 == u)
  454.       ++numthistype;
  455.     /* Only count against fullness if exclusive capacity exceeded. */
  456.     if (numtypes[u2] > ut_capacity_x(u2, t)) {
  457.         fullness += ut_size(u2, t);
  458.     }
  459.     }
  460.     /* Unit can be in this cell if there is dedicated space. */
  461.     if (numthistype + 1 <= utcap)
  462.       return TRUE;
  463.     /* Otherwise decide on the basis of fullness. */
  464.     return (fullness + ut_size(u, t) <= tcap);
  465. }
  466.  
  467. int
  468. type_can_occupy_cell(u, x, y)
  469. int u, x, y;
  470. {
  471.     int t = terrain_at(x, y), u2, u3, numthistype = 0, fullness = 0;
  472.     int tcap, utcap, numtypes[MAXUTYPES];
  473.     Unit *unit2;
  474.  
  475.     tcap = t_capacity(t);
  476.     utcap = ut_capacity_x(u, t);
  477.     if (tcap <= 0 && utcap <= 0)
  478.       return FALSE;
  479.     for_all_unit_types(u3)
  480.       numtypes[u3] = 0;
  481.     for_all_stack(x, y, unit2) {
  482.     u2 = unit2->type;
  483.     ++numtypes[u2];
  484.     if (u2 == u)
  485.       ++numthistype;
  486.     /* Only count against fullness if exclusive capacity exceeded. */
  487.     if (numtypes[u2] > ut_capacity_x(u2, t)) {
  488.         fullness += ut_size(u2, t);
  489.     }
  490.     }
  491.     /* Unit can be in this cell if there is dedicated space. */
  492.     if (numthistype + 1 <= utcap)
  493.       return TRUE;
  494.     /* Otherwise decide on the basis of fullness. */
  495.     return (fullness + ut_size(u, t) <= tcap);
  496. }
  497.  
  498. /* Similar, but don't count a specific given unit when calculating. */
  499.  
  500. int
  501. can_occupy_cell_without(unit, x, y, unit3)
  502. Unit *unit, *unit3;
  503. int x, y;
  504. {
  505.     int u = unit->type, u2, u3, t = terrain_at(x, y), numthistype = 0;
  506.     int fullness = 0, tcap, utcap, numtypes[MAXUTYPES];
  507.     Unit *unit2;
  508.  
  509.     if (unit == NULL)
  510.       run_error("null unit?");  /* should never happen */
  511.     tcap = t_capacity(t);
  512.     utcap = ut_capacity_x(u, t);
  513.     if (tcap <= 0 && utcap <= 0)
  514.       return FALSE;
  515.     for_all_unit_types(u3)
  516.       numtypes[u3] = 0;
  517.     for_all_stack(x, y, unit2) {
  518.     if (unit2 == unit3)
  519.       continue;
  520.     u2 = unit2->type;
  521.     ++numtypes[u2];
  522.     if (u2 == u)
  523.       ++numthistype;
  524.     /* Only count against fullness if exclusive capacity exceeded. */
  525.     if (numtypes[u2] > ut_capacity_x(u2, t)) {
  526.         fullness += ut_size(u2, t);
  527.     }
  528.     }
  529.     /* Unit can be in this cell if there is dedicated space. */
  530.     if (numthistype + 1 <= utcap)
  531.       return TRUE;
  532.     /* Otherwise decide on the basis of fullness. */
  533.     return (fullness + ut_size(u, t) <= tcap);
  534. }
  535.  
  536. int
  537. type_can_occupy_cell_without(u, x, y, unit3)
  538. int u, x, y;
  539. Unit *unit3;
  540. {
  541.     int t = terrain_at(x, y), u2, u3, numthistype = 0, fullness = 0;
  542.     int tcap, utcap;
  543.     int numtypes[MAXUTYPES];
  544.     Unit *unit2;
  545.  
  546.     tcap = t_capacity(t);
  547.     utcap = ut_capacity_x(u, t);
  548.     if (tcap <= 0 && utcap <= 0)
  549.       return FALSE;
  550.     for_all_unit_types(u3)
  551.       numtypes[u3] = 0;
  552.     for_all_stack(x, y, unit2) {
  553.     if (unit2 == unit3)
  554.       continue;
  555.     u2 = unit2->type;
  556.     ++numtypes[u2];
  557.     if (u2 == u)
  558.       ++numthistype;
  559.     /* Only count against fullness if exclusive capacity exceeded. */
  560.     if (numtypes[u2] > ut_capacity_x(u2, t)) {
  561.         fullness += ut_size(u2, t);
  562.     }
  563.     }
  564.     /* Unit can be in this cell if there is dedicated space. */
  565.     if (numthistype + 1 <= utcap)
  566.       return TRUE;
  567.     /* Otherwise decide on the basis of fullness. */
  568.     return (fullness + ut_size(u, t) <= tcap);
  569. }
  570.  
  571. /* Test whether the given position has a connection that the unit may use. */
  572.  
  573. int
  574. can_occupy_conn(unit, nx, ny, nz)
  575. Unit *unit;
  576. int nx, ny, nz;
  577. {
  578.     int c, dir, numhere;
  579.     Unit *unit2;
  580.  
  581.     if (numconntypes == 0)
  582.       return FALSE;
  583.     if (nz & 1 == 1) {
  584.     c = nz / 2;
  585.     } else {
  586.     for_all_terrain_types(c) {
  587.         if (t_is_connection(c) && aux_terrain_defined(c))
  588.           break;
  589.     }
  590.     }
  591.     if (!t_is_connection(c) || !aux_terrain_defined(c))
  592.         run_warning("%s is on an invalid connection type %d?",
  593.             unit_desig(unit), c);
  594.     if (unit->x == nx && unit->y == ny) {
  595.     for_all_directions(dir) {
  596.         if (connection_at(unit->x, unit->y, dir, c))
  597.           break;
  598.     }
  599.     } else {
  600.     dir = closest_dir(nx - unit->x, ny - unit->y);
  601.     }
  602.     if (!connection_at(unit->x, unit->y, dir, c))
  603.       return FALSE;
  604.     numhere = 0;
  605.     for_all_stack(nx, ny, unit2) {
  606.     if (unit->z == nz) {
  607.         ++numhere;
  608.     }
  609.     }
  610.     if (numhere + 1 <= ut_capacity_x(unit->type, c))
  611.       return FALSE;
  612.     /* (should also calc general fullness) */
  613.     return FALSE;
  614. }
  615.  
  616. /* Recursive helper to update everybody's position.  This should be one of
  617.    two routines that modify actual unit positions (leaving is the other). */
  618.  
  619. void
  620. enter_cell_aux(unit, x, y)
  621. Unit *unit;
  622. int x, y;
  623. {
  624.     int u = unit->type;
  625.     Unit *occ;
  626.     Side *side = unit->side;
  627.  
  628. #ifdef DEBUGGING
  629.     /* Not necessarily an error, but indicative of bugs elsewhere. */
  630.     if (unit->x >= 0 || unit->y >= 0) {
  631.     run_warning("unit %d occupying cell (%d, %d), was at (%d %d)",
  632.             unit->id, x, y, unit->x, unit->y);
  633.     }
  634. #endif /* DEBUGGING */
  635.     if (!in_area(x, y))
  636.       run_error("trying to enter cell outside world");
  637.     /* Actually set the unit position. */
  638.     set_unit_position(unit, x, y, unit->z);
  639.     /* Increment viewing coverage, if unit is complete and can see out
  640.        of its transport. */
  641.     if (completed(unit)
  642.         && (unit->transport == NULL
  643.         || uu_occ_can_see(u, unit->transport->type))) {
  644.     cover_area(unit->side, unit, -1, -1, x, y);
  645.     /* If vision range is 0, allow glimpses of adjacent cell terrain.
  646.        This applies to terrain only, adjacent units cannot be seen. */
  647.     if (u_vision_range(u) == 0)
  648.       glimpse_adjacent_terrain(unit);
  649.     }
  650.     /* Do for all the occupants too, recursively. */
  651.     for_all_occupants(unit, occ) {
  652.     enter_cell_aux(occ, x, y);
  653.     }
  654. }
  655.  
  656. /* Decide whether the given unit can actually be in the given transport. */
  657.  
  658. /* (this still needs to account for multipart units) */
  659.  
  660. int
  661. can_occupy(unit, transport)
  662. Unit *transport, *unit;
  663. {
  664.     return can_carry(transport, unit);
  665. }
  666.  
  667. int
  668. can_carry(transport, unit)
  669. Unit *transport, *unit;
  670. {
  671.     int u = unit->type, u2 = transport->type, u3, o;
  672.     int numthistype = 0, numalltypes = 0, occvolume = 0;
  673.     int ucap, uucap;
  674.     int numtypes[MAXUTYPES];
  675.     Unit *occ;
  676.  
  677.     /* Intercept nonsensical arguments. */
  678.     if (transport == unit)
  679.       return FALSE;
  680.     /* Don't allow occupation of incomplete transports unless the unit is
  681.        of a type that can help complete. */
  682.     if (!completed(transport) && uu_acp_to_build(u, u2) < 1)
  683.       return FALSE;
  684.     if (unit->occupant != NULL && !uu_occ_can_have_occs(u, u2))
  685.       return FALSE;
  686.     ucap = u_capacity(u2);
  687.     uucap = uu_capacity_x(u2, u);
  688.     if (ucap <= 0 && uucap <= 0)
  689.       return FALSE;
  690.     for_all_unit_types(u3)
  691.       numtypes[u3] = 0;
  692.     /* Compute the transport's fullness. */
  693.     for_all_occupants(transport, occ) {
  694.         o = occ->type;
  695.     ++numalltypes;
  696.     ++numtypes[occ->type];
  697.     if (o == u)
  698.       ++numthistype;
  699.     /* Only count against fullness if exclusive capacity exceeded. */
  700.     if (numtypes[o] > uu_capacity_x(u2, o)) {
  701.         occvolume += uu_size(o, u2);
  702.     }
  703.     }
  704.     /* Can carry if dedicated space available. */
  705.     if (numthistype + 1 <= uucap)
  706.       return TRUE;
  707.     /* Check upper limit on count of occupants of this type. */
  708.     if (uu_occ_max(u2, u) >= 0
  709.         && numthistype + 1 - uucap > uu_occ_max(u2, u))
  710.       return FALSE;
  711.     /* Check upper limit on count of occupants of all types. */
  712.     if (u_occ_total_max(u2) >= 0
  713.         && numalltypes + 1 > u_occ_total_max(u2))
  714.       return FALSE;
  715.     /* Can carry if general unit hold has room. */
  716.     return (occvolume + uu_size(u, u2) <= ucap);
  717. }
  718.  
  719. /* (should share with prev routine somehow) */
  720.  
  721. int
  722. type_can_occupy(u, transport)
  723. int u;
  724. Unit *transport;
  725. {
  726.     int u2 = transport->type, u3, o;
  727.     int numthistype = 0, numalltypes = 0, occvolume = 0;
  728.     int ucap, uucap;
  729.     int numtypes[MAXUTYPES];
  730.     Unit *occ;
  731.  
  732.     /* Don't allow occupation of incomplete transports unless the unit is
  733.        of a type that can help complete. */
  734.     if (!completed(transport) && uu_acp_to_build(u, u2) < 1)
  735.       return FALSE;
  736.     ucap = u_capacity(u2);
  737.     uucap = uu_capacity_x(u2, u);
  738.     if (ucap <= 0 && uucap <= 0)
  739.       return FALSE;
  740.     for_all_unit_types(u3)
  741.       numtypes[u3] = 0;
  742.     /* Compute the transport's fullness. */
  743.     for_all_occupants(transport, occ) {
  744.         o = occ->type;
  745.     ++numalltypes;
  746.     ++numtypes[o];
  747.     if (o == u)
  748.       ++numthistype;
  749.     /* Only count against fullness if exclusive capacity exceeded. */
  750.     if (numtypes[o] > uu_capacity_x(u2, o)) {
  751.         occvolume += uu_size(o, u2);
  752.     }
  753.     }
  754.     /* Can carry if dedicated space available. */
  755.     if (numthistype + 1 <= uucap)
  756.       return TRUE;
  757.     /* Check upper limit on count of occupants of this type. */
  758.     if (uu_occ_max(u2, u) >= 0
  759.         && numthistype + 1 - uucap > uu_occ_max(u2, u))
  760.       return FALSE;
  761.     /* Check upper limit on count of occupants of all types. */
  762.     if (u_occ_total_max(u2) >= 0
  763.         && numalltypes + 1 > u_occ_total_max(u2))
  764.       return FALSE;
  765.     /* Can carry if general unit hold has room. */
  766.     return (occvolume + uu_size(u, u2) <= ucap);
  767. }
  768.  
  769. int
  770. can_occupy_type(unit, u2)
  771. Unit *unit;
  772. int u2;
  773. {
  774.     int u = unit->type;
  775.  
  776.     /* Can occupy if nonzero reserved capacity for this type. */
  777.     if (uu_capacity_x(u2, u) > 0)
  778.       return TRUE;
  779.     /* Can occupy if general unit hold has room for at least one unit. */
  780.     return (uu_size(u, u2) <= u_capacity(u2));
  781. }
  782.  
  783. int
  784. can_carry_type(transport, u)
  785. Unit *transport;
  786. int u;
  787. {
  788.     return type_can_occupy(u, transport);
  789. }
  790.  
  791. /* Units become occupants by linking into the transport's occupant list. */
  792.  
  793. void
  794. enter_transport(unit, transport)
  795. Unit *unit, *transport;
  796. {
  797.     int u = unit->type, ustack, u2stack;
  798.     Unit *unit2, *topunit, *prevunit = NULL, *nextunit = NULL;
  799.  
  800.     if (unit == transport) {
  801.         run_error("Unit is trying to enter itself");
  802.     }
  803.     topunit = transport->occupant;
  804.     if (topunit) {
  805.         /* Insert the entering unit into the occupant list at
  806.        its correct position. */
  807.         ustack = u_stack_order(u);
  808.         for_all_occupants(transport, unit2) {
  809.         u2stack = u_stack_order(unit2->type);
  810.         if (ustack > u2stack
  811.         || (ustack == u2stack && unit->id < unit2->id)) {
  812.         nextunit = unit2;
  813.         if (unit2 == topunit)
  814.           topunit = unit;
  815.         break;
  816.         }
  817.         prevunit = unit2;
  818.         }
  819.         if (prevunit != NULL)
  820.           prevunit->nexthere = unit;
  821.     } else {
  822.         topunit = unit;
  823.     }
  824.     unit->nexthere = nextunit;
  825.     transport->occupant = topunit;
  826.     /* Point from the unit back to its transport. */
  827.     unit->transport = transport;
  828.     /* If the transport is not yet on the map (such as when patching
  829.        object refs during readin), skip anything that needs the transport's
  830.        location.  It will be handled when the transport is placed. */
  831.     if (inside_area(transport->x, transport->y)) {
  832.     /* Set the passenger's coords to match the transport's. */
  833.     enter_cell_aux(unit, transport->x, transport->y);
  834.     /* Others might be watching. */
  835.     all_see_occupy(unit, transport->x, transport->y, FALSE);
  836.     }
  837. }
  838.  
  839. /* Unit departs from a cell by zeroing out pointer if in cell or by being
  840.    removed from the list of transport occupants. */
  841.  
  842. /* Dead units (hp = 0) may be run through here, so don't error out. */
  843.  
  844. void
  845. leave_cell(unit)
  846. Unit *unit;
  847. {
  848.     int ux = unit->x, uy = unit->y;
  849.     Unit *transport = unit->transport;
  850.  
  851.     if (ux < 0 || uy < 0) {
  852.     /* Sometimes called twice */
  853.     } else if (transport != NULL) {
  854.     leave_transport(unit);
  855.     leave_cell_aux(unit);
  856.     all_see_leave(unit, ux, uy, FALSE);
  857.     /* not all_see_cell here because can't see inside transports */
  858.     update_unit_display(transport->side, transport, TRUE);
  859.     } else {
  860.     remove_unit_from_stack(unit);
  861.     /* Now bash the coords. */
  862.     leave_cell_aux(unit);
  863.     /* Now let everybody observe that the unit is gone. */
  864.     all_see_leave(unit, ux, uy, TRUE);
  865.     }
  866. }
  867.  
  868. /* When leaving, remove view coverage, record old position, and then
  869.    trash the old coordinates just in case.  Catches many bugs.  Do
  870.    this for all the occupants as well. */
  871.  
  872. void
  873. leave_cell_aux(unit)
  874. Unit *unit;
  875. {
  876.     Unit *occ;
  877.  
  878.     if (unit->x < 0 && unit->y < 0) {
  879.     run_warning("unit has already left the cell");
  880.     }
  881.     /* Decrement viewing coverage around our about-to-be-old location. */
  882.     if (completed(unit)
  883.         && (unit->transport == NULL
  884.        || uu_occ_can_see(unit->type, unit->transport->type))) {
  885.     cover_area(unit->side, unit, unit->x, unit->y, -1, -1);
  886.     }
  887.     /* Stash the old coords. */
  888.     unit->prevx = unit->x;  unit->prevy = unit->y;
  889.     /* Set to a recognizable value. */
  890.     unit->x = -1;  unit->y = -1;
  891.     /* Make any occupants leave too. */
  892.     for_all_occupants(unit, occ) {
  893.     leave_cell_aux(occ);
  894.     }
  895. }
  896.  
  897. /* Disembarking unlinks from the list of passengers only, leaves the unit
  898.    hanging in limbo, so should have it occupy something immediately. */
  899.  
  900. void
  901. leave_transport(unit)
  902. Unit *unit;
  903. {
  904.     Unit *transport = unit->transport, *occ;
  905.  
  906.     if (unit == transport) {
  907.         run_error("Unit is trying to leave itself");
  908.     }
  909.     if (unit == transport->occupant) {
  910.     transport->occupant = unit->nexthere;
  911.     } else {
  912.     for_all_occupants(transport, occ) {
  913.         if (unit == occ->nexthere) {
  914.         occ->nexthere = occ->nexthere->nexthere;
  915.         break;
  916.         }
  917.     }
  918.     }
  919.     /* Bash the now-spurious link. */
  920.     unit->transport = NULL;
  921. }
  922.  
  923. int
  924. change_cell(unit, x, y)
  925. Unit *unit;
  926. int x, y;
  927. {
  928.     register int u = unit->type;
  929.     int ux = unit->x, uy = unit->y;
  930.     Unit *transport = unit->transport;
  931.     Side *side = unit->side;
  932.  
  933.     /* Always check this one, but not necessarily fatal. */
  934.     if (!inside_area(x, y)) {
  935.     run_warning("No cell at %d,%d, %s can't enter it",
  936.             x, y, unit_desig(unit));
  937.     /* Let the unit remain off-world. */
  938.     return FALSE;
  939.     }
  940.     if (!can_occupy_cell(unit, x, y)) {
  941.     run_warning("Cell at %d,%d is too full for %s",
  942.             x, y, unit_desig(unit));
  943.     /* Let the unit remain off-world. */
  944.     return FALSE;
  945.     }
  946.     if (transport != NULL) {
  947.     leave_transport(unit);
  948.     update_unit_display(transport->side, transport, TRUE);
  949.     } else {
  950.     remove_unit_from_stack(unit);
  951.     }
  952.     add_unit_to_stack(unit, x, y);
  953.     change_cell_aux(unit, ux, uy, x, y);
  954.     all_see_leave(unit, ux, uy, (transport == NULL));
  955.     /* Inevitable side-effect of appearing in the new location. */
  956.     all_see_occupy(unit, x, y, TRUE);
  957.     return TRUE;
  958. }
  959.  
  960. void
  961. set_unit_position(unit, x, y, z)
  962. Unit *unit;
  963. int x, y;
  964. {
  965.     int u, t, tmpz;
  966.  
  967.     /* Actually set the unit position. */
  968.     unit->x = x;  unit->y = y;  unit->z = z;
  969.     /* Constrain the altitude according to terrain if nonzero. */
  970.     if (unit->z != 0) {
  971.     if (unit->z & 1 == 0) {
  972.         u = unit->type;
  973.         t = terrain_at(x, y);
  974.         tmpz = unit->z / 2;
  975.         tmpz = min(tmpz, ut_alt_max(u, t));
  976.         tmpz = max(tmpz, ut_alt_min(u, t));
  977.         unit->z = tmpz * 2;
  978.     } else {
  979.         /* (should adjust connection type?) */
  980.     }
  981.     }
  982. }
  983.  
  984. static void
  985. change_cell_aux(unit, x0, y0, x, y)
  986. Unit *unit;
  987. int x0, y0, x, y;
  988. {
  989.     int u = unit->type;
  990.     Unit *occ;
  991.  
  992.     /* Stash the old coords. */
  993.     unit->prevx = x0;  unit->prevy = y0;
  994.     set_unit_position(unit, x, y, unit->z);
  995.     /* Change viewing coverage, if unit is complete and can see out
  996.        of its transport. */
  997.     if (completed(unit)
  998.         && (unit->transport == NULL
  999.         || uu_occ_can_see(u, unit->transport->type))) {
  1000.     cover_area(unit->side, unit, x0, y0, x, y);
  1001.     /* If vision range is 0, allow glimpses of adjacent cell terrain.
  1002.        This applies to terrain only, adjacent units cannot be seen. */
  1003.     if (u_vision_range(u) == 0)
  1004.       glimpse_adjacent_terrain(unit);
  1005.     }
  1006.     /* Do for all the occupants too, recursively. */
  1007.     for_all_occupants(unit, occ) {
  1008.     change_cell_aux(occ, x0, y0, x, y);
  1009.     }
  1010. }
  1011.  
  1012. /* Put the given unit into the cell and/or unit stack at the
  1013.    given location.  Do not modify the unit's actual xyz position,
  1014.    just the unit layer and links to other units. */
  1015.  
  1016. void
  1017. add_unit_to_stack(unit, x, y)
  1018. Unit *unit;
  1019. int x, y;
  1020. {
  1021.     int u = unit->type, ustack, u2stack;
  1022.     Unit *topunit, *unit2, *prevunit = NULL, *nextunit = NULL;
  1023.  
  1024.     topunit = unit_at(x, y);
  1025.     if (topunit) {
  1026.         /* Insert the entering unit into the stack at its correct position. */
  1027.         ustack = u_stack_order(u);
  1028.         for_all_stack(x, y, unit2) {
  1029.         u2stack = u_stack_order(unit2->type);
  1030.         if (ustack > u2stack
  1031.         || (ustack == u2stack && unit->id < unit2->id)) {
  1032.         nextunit = unit2;
  1033.         if (unit2 == topunit)
  1034.           topunit = unit;
  1035.         break;
  1036.         }
  1037.         prevunit = unit2;
  1038.         }
  1039.         if (prevunit != NULL)
  1040.       prevunit->nexthere = unit;
  1041.     } else {
  1042.         topunit = unit;
  1043.     }
  1044.     unit->nexthere = nextunit;
  1045.     set_unit_at(x, y, topunit);
  1046. }
  1047.  
  1048. /* Remove the given unit from the stack at its current
  1049.    location. */
  1050.  
  1051. void
  1052. remove_unit_from_stack(unit)
  1053. Unit *unit;
  1054. {
  1055.     int ux = unit->x, uy = unit->y;
  1056.     Unit *other;
  1057.  
  1058.     /* Unsplice ourselves from the list of units in this cell. */
  1059.     if (unit == unit_at(ux, uy)) {
  1060.     set_unit_at(ux, uy, unit->nexthere);
  1061.     } else {
  1062.     for_all_stack(ux, uy, other) {
  1063.         if (unit == other->nexthere) {
  1064.         other->nexthere = other->nexthere->nexthere;
  1065.         break;
  1066.         }
  1067.     } 
  1068.     }
  1069.     /* Bash this now-spurious link. */
  1070.     unit->nexthere = NULL;
  1071. }
  1072.  
  1073. void
  1074. glimpse_adjacent_terrain(unit)
  1075. Unit *unit;
  1076. {
  1077.     int x = unit->x, y = unit->y, dir, x1, y1;
  1078.     Side *side = unit->side;
  1079.  
  1080.     if (u_vision_range(unit->type) == 0
  1081.     && unit->transport == NULL
  1082.     && !all_see_all
  1083.     && !g_terrain_seen()
  1084.     && side != NULL) {
  1085.     for_all_directions(dir) {
  1086.         if (point_in_dir(x, y, dir, &x1, &y1)) {
  1087.             if (terrain_view(side, x1, y1) == UNSEEN) {
  1088.             set_terrain_view(side, x1, y1,
  1089.                      buildtview(terrain_at(x1, y1)));
  1090.             update_cell_display(side, x1, y1, TRUE);
  1091.             }
  1092.         }
  1093.     }
  1094.     }
  1095. }
  1096.  
  1097. /* Given an overfull unit, spew out occupants until back within limits. */
  1098.  
  1099. void
  1100. eject_excess_occupants(unit)
  1101. Unit *unit;
  1102. {
  1103.     int u, u2 = unit->type, overfull = TRUE, count;
  1104.     int numalltypes = 0, occvolume = 0;
  1105.     int numeachtype[MAXUTYPES], sharedeachtype[MAXUTYPES];
  1106.     Unit *occ;
  1107.  
  1108.     for_all_unit_types(u)
  1109.       numeachtype[u] = sharedeachtype[u] = 0;
  1110.     /* Eject occupants overflowing counts in shared space. */
  1111.     for_all_occupants(unit, occ)
  1112.       ++numeachtype[occ->type];
  1113.     for_all_unit_types(u) {
  1114.         if (numeachtype[u] > uu_capacity_x(u2, u)) {
  1115.         sharedeachtype[u] = numeachtype[u] - uu_capacity_x(u2, u);
  1116.         if (uu_occ_max(u2, u) >= 0
  1117.             && sharedeachtype[u] > uu_occ_max(u2, u)) {
  1118.                 count = sharedeachtype[u] - uu_occ_max(u2, u);
  1119.                 while (count > 0) {
  1120.                     for_all_occupants(unit, occ) {
  1121.                         if (occ->type == u) {
  1122.                             eject_occupant(unit, occ);
  1123.                             --count;
  1124.                             break;
  1125.                         }
  1126.                     }
  1127.                 }
  1128.         }
  1129.     }
  1130.     }
  1131.     /* Eject occupants over the total max count allowed. */
  1132.     for_all_occupants(unit, occ)
  1133.       ++numalltypes;
  1134.     if (u_occ_total_max(u2) >= 0 && numalltypes > u_occ_total_max(u2)) {
  1135.         count = numalltypes - u_occ_total_max(u2);
  1136.         while (unit->occupant != NULL) {
  1137.         eject_occupant(unit, unit->occupant);
  1138.         if (--count <= 0)
  1139.           break;
  1140.         }
  1141.     }
  1142.     /* Eject occupants overflowing volume of shared space. */
  1143.     while (overfull) {
  1144.     for_all_unit_types(u)
  1145.       numeachtype[u] = 0;
  1146.     occvolume = 0;
  1147.     for_all_occupants(unit, occ)
  1148.       ++numeachtype[occ->type];
  1149.     for_all_unit_types(u) {
  1150.         occvolume +=
  1151.           max(0, numeachtype[u] - uu_capacity_x(u2, u)) * uu_size(u, u2);
  1152.     }
  1153.     if (occvolume > u_capacity(u2)) {
  1154.         overfull = TRUE;
  1155.         eject_occupant(unit, unit->occupant);
  1156.     } else {
  1157.         overfull = FALSE;
  1158.     }
  1159.     }
  1160. }
  1161.  
  1162. /* Given that an occupant must leave its transport, decide what happens; either
  1163.    move out into the open, into another unit, or vanish. */
  1164.  
  1165. /* (should be generic test) */
  1166. #define ut_dies_on(u, t) (ut_vanishes_on(u,t) || ut_wrecks_on(u, t))
  1167.  
  1168. void
  1169. eject_occupant(unit, occ)
  1170. Unit *unit, *occ;
  1171. {
  1172.     if (!in_play(unit) || !in_play(occ))
  1173.       return;
  1174.     /* If the occupant is mobile and the current cell has room, let it escape
  1175.        but be stacked in the transport's cell. */
  1176.     if (mobile(occ->type)
  1177.         && !ut_dies_on(occ->type, terrain_at(unit->x, unit->y))
  1178.         && can_occupy_cell(occ, unit->x, unit->y)) {
  1179.         leave_cell(occ);
  1180.         enter_cell(occ, unit->x, unit->y);
  1181.         return;
  1182.     }
  1183.     /* (should let occupants escape into other units in cell) */
  1184.     /* (should let occupants with acp escape into adj cells) */
  1185.     /* Evaporating the occupant is our last option. */
  1186.     kill_unit(occ, H_UNIT_KILLED);
  1187. }
  1188.  
  1189. /* Handle the general situation of a unit changing allegiance from one side
  1190.    to another.  This is a common internal routine, so no messages here. */
  1191.  
  1192. void
  1193. change_unit_side(unit, newside, reason, agent)
  1194. Unit *unit, *agent;
  1195. Side *newside;
  1196. int reason;
  1197. {
  1198.     int ux = unit->x, uy = unit->y;
  1199.     Side *oldside = unit->side;
  1200.     Unit *occ;
  1201.  
  1202.     if (newside == indepside) {
  1203.     run_warning("non-null indepside");
  1204.     newside = NULL;
  1205.     }
  1206.     if (oldside == newside)
  1207.       return;
  1208.     /* Fail if the unit may not be on the new side. */
  1209.     if (!unit_allowed_on_side(unit, newside))
  1210.     return;
  1211.     if (reason >= 0)
  1212.       record_unit_side_change(unit, newside, reason, agent);
  1213.     if (oldside != NULL) {
  1214.     /* Last view of unit on its old side. */
  1215.     update_unit_display(oldside, unit, TRUE);
  1216.     }
  1217.     /* (Should this be switchable maybe?) */
  1218.     for_all_occupants(unit, occ) {
  1219.     change_unit_side(occ, newside, reason, agent);
  1220.     }
  1221.     /* Adjust view coverage.  The sequencing here is to make sure that no
  1222.        viewing coverage gets left on or off inadvertantly. */
  1223.     if (alive(unit) && inside_area(ux, uy)) {
  1224.     /* Uncover the current viewed area. */
  1225.     cover_area(unit->side, unit, ux, uy, -1, -1);
  1226.     /* Actually set the side slot of the unit here. */
  1227.     set_unit_side(unit, newside);
  1228.     /* Cover it for the new side now. */
  1229.     cover_area(unit->side, unit, -1, -1, ux, uy);
  1230.     /* A freebie for the unit's previous side. */
  1231.     see_exact(oldside, ux, uy);
  1232.     }
  1233.     /* Reflect the changeover in any appropriate displays. */
  1234.     if (oldside != NULL) {
  1235.     /* Now we see the unit as belonging to someone else. */
  1236.     update_unit_display(oldside, unit, TRUE);
  1237.     }
  1238.     if (newside != NULL) {
  1239.     update_unit_display(newside, unit, TRUE);
  1240.     }
  1241. }
  1242.  
  1243. /* This is a general test as to whether the given unit can be
  1244.    on the given side. */
  1245.  
  1246. int
  1247. unit_allowed_on_side(unit, side)
  1248. Unit *unit;
  1249. Side *side;
  1250. {
  1251.     int u;
  1252.  
  1253.     if (unit == NULL)
  1254.       return FALSE;
  1255.     u = unit->type;
  1256.     /* Test general limitations on the type. */
  1257.     if (!type_allowed_on_side(u, side))
  1258.       return FALSE;
  1259.     /* Test specific game limits. */
  1260.     if (u_type_per_side_max(u) >= 0) {
  1261.     if (side->numunits[u] >= u_type_per_side_max(u))
  1262.       return FALSE;
  1263.     }
  1264.     if (g_units_per_side_max() >= 0) {
  1265.     int u2, sum;
  1266.  
  1267.     sum = 0;
  1268.     for_all_unit_types(u2) {
  1269.         sum += side->numunits[u2];
  1270.     }
  1271.     if (sum >= g_units_per_side_max())
  1272.       return FALSE;
  1273.     }
  1274.     return TRUE;
  1275. }
  1276.  
  1277. int
  1278. test_class_membership(leaf)
  1279. Obj *leaf;
  1280. {
  1281.     char *sclass;
  1282.  
  1283.     if (stringp(leaf)) {
  1284.     sclass = c_string(leaf);
  1285.     if (tmpside_classtest != NULL && tmpside_classtest != indepside) {
  1286.         if (empty_string(tmpside_classtest->sideclass))
  1287.           return FALSE;
  1288.         return (strcmp(sclass, tmpside_classtest->sideclass) == 0);
  1289.     } else {
  1290.         return (strcmp(sclass, "independent") == 0);
  1291.     }
  1292.     } else {
  1293.     init_warning("testing side against garbled class expression");
  1294.     /* Be permissive if continued. */
  1295.     return TRUE;
  1296.     }
  1297. }
  1298.  
  1299. int
  1300. type_allowed_on_side(u, side)
  1301. int u;
  1302. Side *side;
  1303. {
  1304.     if (side == NULL)
  1305.       side = indepside;
  1306.     if (side->uavail == NULL) {
  1307.     int u2;
  1308.  
  1309.     side->uavail = (short *) xmalloc(numutypes * sizeof(short));
  1310.     for_all_unit_types(u2) {
  1311.         tmpside_classtest = side;
  1312.         side->uavail[u2] =
  1313.           eval_boolean_expression(u_possible_sides(u2), test_class_membership, TRUE);
  1314.     }
  1315.     }
  1316.     return side->uavail[u];
  1317. }
  1318.  
  1319. int
  1320. unit_trusts_unit(unit1, unit2)
  1321. Unit *unit1, *unit2;
  1322. {
  1323.     if (unit1->side == NULL || unit2->side == NULL)
  1324.       return FALSE;
  1325.     /* (should eventually be possible for some indeps to trust) */
  1326.     return (unit1->side == unit2->side
  1327.         || trusted_side(unit1->side, unit2->side));
  1328. }
  1329.  
  1330. /* Put the given unit on the given side, without all the fancy effects.
  1331.    Important to handle independents, because this gets called during init.
  1332.    This is the only way that a unit's side may be altered. */
  1333.  
  1334. /* Note that this may be run on dead units, as part of clearing out a
  1335.    side's units, in which case we just want to relink, don't care about
  1336.    testing whether the type is allowed or not. */
  1337.  
  1338. int
  1339. set_unit_side(unit, side)
  1340. Unit *unit;
  1341. Side *side;
  1342. {
  1343.     Side *oldside, *newside;
  1344.  
  1345.     if (side == indepside)
  1346.       side = NULL;
  1347.     if (unit->side != side) {
  1348.     /* Subtract from the counts for the ex-side. */
  1349.     oldside = (unit->side ? unit->side : indepside);
  1350.     if (oldside->numunits)
  1351.       --(oldside->numunits[unit->type]);
  1352.     if (oldside->numlive && in_play(unit))
  1353.       --(oldside->numlive[unit->type]);
  1354.     /* Set the unit's slot. */
  1355.     /* Note that indep units have a NULL side, even though there
  1356.        is an actual side object for independents. */
  1357.     unit->side = side;
  1358.     /* Make sure this unit is off anybody else's list. */
  1359.     delete_unit(unit);
  1360.     newside = (side ? side : indepside);
  1361.     insert_unit(newside->unithead, unit);
  1362.     /* Add to counts for the side. */
  1363.     if (newside->numunits)
  1364.       ++(newside->numunits[unit->type]);
  1365.     if (newside->numlive && in_play(unit))
  1366.       ++(newside->numlive[unit->type]);
  1367.     /* Invalidate both sides' point value caches. */
  1368.     oldside->point_value_valid = FALSE;
  1369.     newside->point_value_valid = FALSE;
  1370.     }
  1371.     return TRUE;
  1372. }
  1373.  
  1374. int
  1375. set_unit_origside(unit, side)
  1376. Unit *unit;
  1377. Side *side;
  1378. {
  1379.     if (unit->origside != side) {
  1380.     /* Set the unit's slot. */
  1381.     unit->origside = side;
  1382.     }
  1383.     return TRUE;
  1384. }
  1385.  
  1386. void
  1387. set_unit_name(side, unit, newname)
  1388. Side *side;
  1389. Unit *unit;
  1390. char *newname;
  1391. {
  1392.     /* Always turn 0-length names into NULL. */
  1393.     if (newname != NULL && strlen(newname) == 0)
  1394.       newname = NULL;
  1395.     /* Don't do anything if the name didn't actually change. */
  1396.     if ((unit->name == NULL && newname == NULL)
  1397.         || (unit->name != NULL
  1398.         && newname != NULL
  1399.         && strcmp(unit->name, newname) == 0))
  1400.       return;
  1401.     /* Record this in the history. */
  1402.     record_unit_name_change(unit, newname);
  1403.     unit->name = newname;
  1404.     update_unit_display(side, unit, TRUE);
  1405.     update_unit_display(unit->side, unit, TRUE);
  1406.     /* (should also send to any other side directly viewing this unit!) */
  1407. }
  1408.  
  1409. int
  1410. disband_unit_directly(side, unit)
  1411. Side *side;
  1412. Unit *unit;
  1413. {
  1414.     if (side_can_disband(side, unit)) {
  1415.     if (!completed(unit)) {
  1416.         /* Nothing complicated about getting rid of an incomplete unit. */
  1417.         kill_unit(unit, H_UNIT_DISBANDED);
  1418.         return TRUE;
  1419.     } else {
  1420.         return FALSE;
  1421.     }
  1422.     } else {
  1423.     return FALSE;
  1424.     }
  1425. }
  1426.  
  1427. /* Remove a unit from play.  This is different from making it available for
  1428.    reallocation - only the unit flusher can do that.  We remove all the
  1429.    passengers too, recursively.  Sometimes units are "killed twice", so
  1430.    be sure not to run all this twice.  Also count up occupant deaths, being
  1431.    sure not to count the unit itself as an occupant. */
  1432.  
  1433. void
  1434. kill_unit(unit, reason)
  1435. Unit *unit;
  1436. int reason;
  1437. {
  1438.     int u = unit->type, selfdied = FALSE;
  1439.     int ux = unit->x, uy = unit->y;
  1440.  
  1441.     if (alive(unit)) {
  1442.     if (unit->side && unit->side->self_unit == unit)
  1443.       selfdied = TRUE;
  1444.     leave_cell(unit);
  1445.     /* A freebie for the unit's side. */
  1446.     see_exact(unit->side, ux, uy);
  1447.     kill_unit_aux(unit, reason);
  1448.     if (selfdied) {
  1449.         if (u_self_resurrects(u)) {
  1450.         /* should find and designate a new self unit */
  1451.         return;
  1452.         } else {
  1453.         /* Make sure this doesn't have serious consequences? */
  1454.         if (unit->side->ingame)
  1455.           side_loses(unit->side, NULL, -2);
  1456.         /* (can't do all consequences just yet?) */
  1457.         }
  1458.     }
  1459.     recent_dead_flushed = FALSE;
  1460.     }
  1461. }
  1462.  
  1463. /* Trash it now - occupant doesn't need to leave_cell.  Also record the
  1464.    event, and update the apropriate display.  The unit here should
  1465.    be known to be alive. */
  1466.  
  1467. void
  1468. kill_unit_aux(unit, reason)
  1469. Unit *unit;
  1470. int reason;
  1471. {
  1472.     int u = unit->type;
  1473.     Unit *occ;
  1474.     Side *side = unit->side;
  1475.     
  1476.     unit->hp = 0;
  1477.     /* Get rid of the unit's plan/tasks.  This should be safe, because
  1478.        unit death should only happen during action execution and in
  1479.        between turns, and plans/tasks should not be in use at those times. */
  1480.     dispose_of_plan(unit);
  1481.     /* Maybe enter the loss into the historical record. */
  1482.     if (reason >= 0)
  1483.       record_unit_death(unit, reason);
  1484.     remove_unit_from_vector((unit->side ? unit->side : indepside)->actionvector, unit, -1);
  1485.     if (side != NULL) {
  1486.     /* Invalidate the side's point value cache. */
  1487.     side->point_value_valid = FALSE;
  1488.     if (side_has_ai(side)) {
  1489.         ai_react_to_unit_loss(side, unit);
  1490.     }
  1491.     update_unit_display(side, unit, TRUE);
  1492.     }
  1493.     /* Kill all the occupants in turn. */
  1494.     for_all_occupants(unit, occ) {
  1495.     if (alive(occ))
  1496.       kill_unit_aux(occ, reason);
  1497.     }
  1498.     /* Update global counts. */
  1499.     --numliveunits;
  1500.     --(numlivebytype[u]);
  1501. }
  1502.  
  1503. /* Get rid of all dead units at once.
  1504.    (This routine is basically a garbage collector, and should not be called
  1505.    during a unit list traversal.) The process starts by finding the first
  1506.    live unit, making it the head, then linking around all in the middle.
  1507.    Dead units stay on the dead unit list for each side until that side has had
  1508.    a chance to move.  Then they are finally flushed in a permanent fashion. */ 
  1509.  
  1510. static void flush_one_unit PARAMS ((Unit *unit));
  1511.  
  1512. void
  1513. flush_dead_units()
  1514. {
  1515.     Unit *unit, *prevunit, *nextunit;
  1516.  
  1517.     if (unitlist == NULL)
  1518.       return;
  1519.     unit = unitlist;
  1520.     while (!alive(unit)) {
  1521.     nextunit = unit->unext;
  1522.     delete_unit(unit);
  1523.     flush_one_unit(unit);
  1524.     unit = nextunit;
  1525.     }
  1526.     unitlist = unit;
  1527.     /* Since the first unit of unitlist is guaranteed live now,
  1528.        we know that prevunit will always be set correctly;
  1529.        but mollify insufficiently intelligent compilers. */
  1530.     prevunit = NULL;
  1531.     for_all_units(unit) {
  1532.     if (!alive(unit)) {
  1533.         nextunit = unit->unext;
  1534.         prevunit->unext = unit->unext;
  1535.         delete_unit(unit);
  1536.         flush_one_unit(unit);
  1537.         unit = prevunit;
  1538.     } else {
  1539.         prevunit = unit;
  1540.     }
  1541.     }
  1542. }
  1543.  
  1544. /* Keep it clean - hit all links to other places.  Some might not be
  1545.    strictly necessary, but this is not an area to take chances with. */
  1546.  
  1547. static void
  1548. flush_one_unit(unit)
  1549. Unit *unit;
  1550. {
  1551.     unit->id = -1;
  1552.     unit->occupant = NULL;
  1553.     unit->transport = NULL;
  1554.     unit->nexthere = NULL;
  1555.     unit->prev = NULL;
  1556.     unit->unext = NULL;
  1557.     /* Add it on the front of the list of available units. */
  1558.     unit->next = freeunits;
  1559.     freeunits = unit;
  1560. }
  1561.  
  1562. /* Do a bubble sort.
  1563.    Data is generally coherent, so bubble sort not too bad if we allow
  1564.    early termination when everything is already in order.  */
  1565.  
  1566. /* If slowness objectionable, replace with something clever, but be
  1567.    sure that average performance in real games is what's being improved. */
  1568.  
  1569. void
  1570. sort_units()
  1571. {
  1572.     int flips;
  1573.     int passes = 0;
  1574.     register Unit *unit, *nextunit;
  1575.     Side *side;
  1576.  
  1577.     for_all_sides_plus_indep(side) {
  1578.     passes = 0;
  1579.     flips = TRUE;
  1580.     while (flips) {
  1581.         flips = FALSE;
  1582.         for_all_side_units(side, unit) {
  1583.         if (unit->next != side->unithead
  1584.             && compare_units(unit, unit->next) > 0) {
  1585.             flips = TRUE;
  1586.             /* Reorder the units by fiddling with their links. */
  1587.             nextunit = unit->next;
  1588.             unit->prev->next = nextunit;
  1589.             nextunit->next->prev = unit;
  1590.             nextunit->prev = unit->prev;
  1591.             unit->next = nextunit->next;
  1592.             nextunit->next = unit;
  1593.             unit->prev = nextunit;
  1594.         }
  1595.         ++passes;
  1596.         }
  1597.     }
  1598.     }
  1599.     Dprintf("Sorting passes = %d\n", passes);
  1600. }
  1601.  
  1602. static int
  1603. compare_units(unit1, unit2)
  1604. Unit *unit1, *unit2;
  1605. {
  1606.     if (unit1->type != unit2->type)
  1607.       return (unit1->type - unit2->type);
  1608.     if (unit1->name && unit2->name == NULL)
  1609.       return -1;
  1610.     if (unit1->name == NULL && unit2->name)
  1611.       return 1;
  1612.     if (unit1->name && unit2->name)
  1613.       return strcmp(unit1->name, unit2->name);
  1614.     if (unit1->number != unit2->number)
  1615.       return (unit1->number - unit2->number);
  1616.     /* Ids impose a total ordering. */
  1617.     return (unit1->id - unit2->id);
  1618. }
  1619.  
  1620. /* Useful for the machine player to know how long it can move this
  1621.    piece before it should go home.  Assumes can't replenish from
  1622.    terrain.  Result may be negative, in which case it's time to go! */
  1623.  
  1624. int
  1625. moves_till_low_supplies(unit)
  1626. Unit *unit;
  1627. {
  1628.     int u = unit->type, m, moves = 1234567, tmp;
  1629.  
  1630.     for_all_material_types(m) {
  1631.     if ((um_consumption_per_move(u, m) > 0)) {
  1632.         tmp = (unit->supply[m] - um_storage_x(u, m) / 2) / um_consumption_per_move(u, m);
  1633.         moves = min(moves, tmp);
  1634.     }
  1635.     }
  1636.     return moves;
  1637. }
  1638.  
  1639. /* Short, unreadable, but greppable listing of unit.  Primarily useful
  1640.    for debugging and warnings.  We use several buffers and rotate between
  1641.    them so we can call this more than once in a single printf. */
  1642.  
  1643. char *
  1644. unit_desig(unit)
  1645. Unit *unit;
  1646. {
  1647.     int i;
  1648.     char *shortbuf;
  1649.  
  1650.     /* Allocate if not yet done so. */
  1651.     for (i = 0; i < NUMSHORTBUFS; ++i) {
  1652.     if (shortbufs[i] == NULL)
  1653.       shortbufs[i] = xmalloc(BUFSIZE);
  1654.     }
  1655.     /* Note that we test here, so that unit_desig(NULL) can be used
  1656.        to allocate any space that this routine might need later. */
  1657.     if (unit == NULL)
  1658.       return "no unit";
  1659.     shortbuf = shortbufs[curshortbuf];
  1660.     curshortbuf = (curshortbuf + 1) % NUMSHORTBUFS;
  1661.     if (unit->id == -1) {
  1662.     sprintf(shortbuf, "s%d head", side_number(unit->side));
  1663.     return shortbuf;
  1664.     } else if (is_unit_type(unit->type)) {
  1665.     sprintf(shortbuf, "s%d %-3.3s %d (%d,%d",
  1666.         side_number(unit->side), shortest_unique_name(unit->type),
  1667.         unit->id, unit->x, unit->y);
  1668.     if (unit->z != 0)
  1669.       tprintf(shortbuf, ",%d", unit->z);
  1670.     /* This has the potential to generate some very misleading
  1671.        results, if this routine is called on a unit before its
  1672.        transport field is patched from a Lisp object into a real
  1673.        unit reference. */
  1674.     if (unit->transport)
  1675.       tprintf(shortbuf, ",in%d", unit->transport->id);
  1676.     strcat(shortbuf, ")");  /* close out the unit location */
  1677.     return shortbuf;
  1678.     } else {
  1679.     return "!garbage unit!";
  1680.     }
  1681. }
  1682.  
  1683. /* Short, unreadable, but greppable listing of unit that omits anything
  1684.    that changes from turn to turn. */
  1685.  
  1686. char *
  1687. unit_desig_no_loc(unit)
  1688. Unit *unit;
  1689. {
  1690.     char *shortbuf;
  1691.  
  1692.     if (unit == NULL)
  1693.       return "no unit";
  1694.     /* Allocate if not yet done so. */
  1695.     if (shortbufs[curshortbuf] == NULL)
  1696.       shortbufs[curshortbuf] = xmalloc(BUFSIZE);
  1697.     shortbuf = shortbufs[curshortbuf];
  1698.     curshortbuf = (curshortbuf + 1) % NUMSHORTBUFS;
  1699.     if (unit->id == -1) {
  1700.     sprintf(shortbuf, "s%d head", side_number(unit->side));
  1701.     return shortbuf;
  1702.     } else if (is_unit_type(unit->type)) {
  1703.     sprintf(shortbuf, "s%d %-3.3s %d",
  1704.         side_number(unit->side), shortest_unique_name(unit->type),
  1705.         unit->id);
  1706.     return shortbuf;
  1707.     } else {
  1708.     return "!garbage unit!";
  1709.     }
  1710. }
  1711.  
  1712. /* Come up with a unit type name that fits in the given space. */
  1713.  
  1714. char *
  1715. utype_name_n(u, n)
  1716. int u, n;
  1717. {
  1718.     char *utypename, *shortname, *rawname;
  1719.  
  1720.     utypename = u_type_name(u);
  1721.     if (n <= 0 || strlen(utypename) <= n) {
  1722.     return utypename;
  1723.     } else if (n == 1 && !empty_string(u_uchar(u))) {
  1724.     /* Use the unit char if possible. */
  1725.     return u_uchar(u);
  1726.     } else if (!empty_string(u_short_name(u))) {
  1727.         shortname = u_short_name(u);
  1728.     if (strlen(shortname) <= n) {
  1729.         return shortname;
  1730.     } else {
  1731.         rawname = shortname;
  1732.     }
  1733.     } else {
  1734.         rawname = utypename;
  1735.     }
  1736.     /* Copy what will fit. */
  1737.     strncpy(utypenamen, rawname, n);
  1738.     utypenamen[n] = '\0';
  1739.     return utypenamen;
  1740. }
  1741.  
  1742. char **shortestnames = NULL;
  1743.  
  1744. char *
  1745. shortest_unique_name(u)
  1746. int u;
  1747. {
  1748.     char namebuf[BUFSIZE], *name1;
  1749.     int u1, u2, i, allhavechars, firstuniq[MAXUTYPES], firstuniq1;
  1750.  
  1751.     if (shortestnames == NULL) {
  1752.     shortestnames = (char **) xmalloc(numutypes * sizeof(char *));
  1753.     allhavechars = TRUE;
  1754.     for_all_unit_types(u1) {
  1755.         if (!empty_string(u_uchar(u1))) {
  1756.         namebuf[0] = (u_uchar(u1))[0];
  1757.         namebuf[1] = '\0';
  1758.         shortestnames[u1] = copy_string(namebuf);
  1759.         } else {
  1760.         allhavechars = FALSE;
  1761.         }
  1762.     }
  1763.     if (!allhavechars) {
  1764.         for_all_unit_types(u1) {
  1765.         shortestnames[u1] = copy_string(u_type_name(u1));
  1766.         firstuniq[u1] = 0;
  1767.         }
  1768.         for_all_unit_types(u1) {
  1769.         name1 = shortestnames[u1];
  1770.         firstuniq1 = firstuniq[u1];
  1771.         for_all_unit_types(u2) {
  1772.             if (u1 != u2) {
  1773.             for (i = 0; i < firstuniq1; ++i ) {
  1774.                 if (name1[i] != (shortestnames[u2])[i]) {
  1775.                 break;
  1776.                 }
  1777.             }
  1778.             if (i == firstuniq1) {
  1779.                 while (name1[firstuniq1] == (shortestnames[u2])[firstuniq1]) {
  1780.                 ++firstuniq1;
  1781.                 }
  1782.             }
  1783.             }
  1784.         }
  1785.         firstuniq[u1] = firstuniq1;
  1786.         }
  1787.         for_all_unit_types(u1) {
  1788.         if (firstuniq[u1] + 1 < strlen(shortestnames[u1])) {
  1789.             (shortestnames[u1])[firstuniq[u1] + 1] = '\0';
  1790.         }
  1791.         }
  1792.     }
  1793.     }
  1794.     return shortestnames[u];
  1795. }
  1796.  
  1797. /* This formats an actorstate readably. */
  1798.  
  1799. char *
  1800. actorstate_desig(as)
  1801. ActorState *as;
  1802. {
  1803.     if (actorstatebuf == NULL)
  1804.       actorstatebuf = xmalloc(BUFSIZE);
  1805.     if (as != NULL) {
  1806.     sprintf(actorstatebuf, "acp %d/%d %s",
  1807.         as->acp, as->initacp, action_desig(&(as->nextaction)));
  1808.     return actorstatebuf;
  1809.     } else {
  1810.     return "no act";
  1811.     }
  1812. }
  1813.  
  1814. /* Search for a unit with the given id number. */
  1815.  
  1816. /* This is used a lot, it should be sped up. */
  1817.  
  1818. Unit *
  1819. find_unit(n)
  1820. int n;
  1821. {
  1822.     Unit *unit;
  1823.  
  1824.     for_all_units(unit) {
  1825.     if (alive(unit) && unit->id == n)
  1826.       return unit;
  1827.     }
  1828.     return NULL;
  1829. }
  1830.  
  1831. /* Find a unit with the given name, either alive or dead. */
  1832.  
  1833. Unit *
  1834. find_unit_by_name(nm)
  1835. char *nm;
  1836. {
  1837.     Unit *unit;
  1838.  
  1839.     if (nm == NULL)
  1840.       return NULL;
  1841.     for_all_units(unit) {
  1842.     if (unit->name != NULL && strcmp(unit->name, nm) == 0)
  1843.       return unit;
  1844.     }
  1845.     return NULL;
  1846. }
  1847.  
  1848. /* Find a unit with the given number, either alive or dead. */
  1849.  
  1850. Unit *
  1851. find_unit_by_number(nb)
  1852. int nb;
  1853. {
  1854.     Unit *unit;
  1855.  
  1856.     for_all_units(unit) {
  1857.     if (unit->number == nb)
  1858.       return unit;
  1859.     }
  1860.     return NULL;
  1861. }
  1862.  
  1863. Unit *
  1864. find_unit_dead_or_alive(n)
  1865. int n;
  1866. {
  1867.     Unit *unit;
  1868.  
  1869.     for_all_units(unit) {
  1870.     if (unit->id == n)
  1871.       return unit;
  1872.     }
  1873.     return NULL;
  1874. }
  1875.  
  1876. /* Given a name, find the type. */
  1877.  
  1878. int
  1879. find_unit_name(str)
  1880. char *str;
  1881. {
  1882.     int u;
  1883.  
  1884.     for_all_unit_types(u)
  1885.       if (strcmp(str, u_short_name(u)) == 0
  1886.       || strcmp(str, u_type_name(u)) == 0) 
  1887.     return u;
  1888.     return NONUTYPE;
  1889. }
  1890.  
  1891. /* Insert the given unit after the other given unit. */
  1892.  
  1893. void
  1894. insert_unit(unithead, unit)
  1895. Unit *unithead, *unit;
  1896. {
  1897.     unit->next = unithead->next;
  1898.     unit->prev = unithead;
  1899.     unithead->next->prev = unit;
  1900.     unithead->next = unit;
  1901. }
  1902.  
  1903. /* Delete the unit from its list. */
  1904.  
  1905. void
  1906. delete_unit(unit)
  1907. Unit *unit;
  1908. {
  1909.     unit->next->prev = unit->prev;
  1910.     unit->prev->next = unit->next;
  1911. }
  1912.  
  1913. int
  1914. num_occupants(unit)
  1915. Unit *unit;
  1916. {
  1917.     int num = 0;
  1918.     Unit *occ;
  1919.  
  1920.     for_all_occupants(unit, occ) {
  1921.     num += 1;
  1922.     }
  1923.     return num;
  1924. }
  1925.  
  1926. int
  1927. num_units_at(x, y)
  1928. int x, y;
  1929. {
  1930.     int num = 0;
  1931.     Unit *unit;
  1932.  
  1933.     x = wrapx(x);
  1934.     if (!in_area(x, y)) {
  1935.     run_warning("num_units_at %d,%d??", x, y);
  1936.     return 0;
  1937.     }
  1938.     for_all_stack(x, y, unit) {
  1939.     num += 1;
  1940.     }
  1941.     return num;
  1942. }
  1943.  
  1944. /* Call this to doublecheck invariants on units. */
  1945.  
  1946. void
  1947. check_all_units()
  1948. {
  1949.     Unit *unit;
  1950.  
  1951.     for_all_units(unit) {
  1952.     check_unit(unit);
  1953.     }
  1954. }
  1955.  
  1956. void
  1957. check_unit(unit)
  1958. Unit *unit;
  1959. {
  1960.     if (alive(unit) && unit->transport && !alive(unit->transport)) {
  1961.         run_warning("%s is inside a dead transport", unit_desig(unit));
  1962.     }
  1963.     /* etc */
  1964. }
  1965.  
  1966. UnitVector *
  1967. make_unit_vector(initsize)
  1968. int initsize;
  1969. {
  1970.     UnitVector *vec;
  1971.     
  1972.     vec = (UnitVector *)
  1973.       xmalloc(sizeof(UnitVector) + initsize * sizeof(UnitVectorEntry));
  1974.     vec->size = initsize;
  1975.     vec->numunits = 0;
  1976.     return vec;
  1977. }
  1978.  
  1979. void
  1980. clear_unit_vector(vec)
  1981. UnitVector *vec;
  1982. {
  1983.     vec->numunits = 0;
  1984. }
  1985.  
  1986. UnitVector *
  1987. add_unit_to_vector(vec, unit, flag)
  1988. UnitVector *vec;
  1989. Unit *unit;
  1990. int flag;
  1991. {
  1992.     int i;
  1993.     UnitVector *newvec;
  1994.  
  1995.     /* Can't add to something that doesn't exist! */
  1996.     if (vec == NULL)
  1997.       run_error("No actionvector!");
  1998.     /* (should search to see if already present) */
  1999.     if (vec->numunits >= vec->size) {
  2000.     newvec = make_unit_vector((3 * vec->size) / 2);
  2001.     newvec->numunits = vec->numunits;
  2002.     for (i = 0; i < vec->numunits; ++i) {
  2003.         newvec->units[i] = vec->units[i];
  2004.     }
  2005.     vec = newvec;
  2006.     }
  2007.     ((vec->units)[vec->numunits]).unit = unit;
  2008.     ((vec->units)[vec->numunits]).flag = flag;
  2009.     ++(vec->numunits);
  2010.     return vec;
  2011. }
  2012.  
  2013. void
  2014. remove_unit_from_vector(vec, unit, pos)
  2015. UnitVector *vec;
  2016. Unit *unit;
  2017. int pos;
  2018. {
  2019.     int j;
  2020.  
  2021.     /* It's probably a bug that the vector is null sometimes,
  2022.        but don't flip out over it. */
  2023.     if (vec == NULL)
  2024.       return;
  2025.     /* Search for unit in vector. */
  2026.     if (pos < 0) {
  2027.     for (j = 0; j < vec->numunits; ++j) {
  2028.         if (unit == vec->units[j].unit) {
  2029.         pos = j;
  2030.         break;
  2031.         }
  2032.     }
  2033.     }
  2034.     if (pos < 0)
  2035.       return;
  2036.     if (unit != vec->units[pos].unit)
  2037.       run_error("unit mismatch in remove_unit_from_vector, %s not at %d",
  2038.         unit_desig(unit), pos);    
  2039.     for (j = pos + 1; j < vec->numunits; ++j)
  2040.       vec->units[j-1] = vec->units[j];
  2041.     --(vec->numunits);
  2042. }
  2043.  
  2044. enum sortkeys tmpsortkeys[MAXSORTKEYS];
  2045.  
  2046. static int
  2047. compare_units_by_keys(e1, e2)
  2048. CONST void *e1, *e2;
  2049. {
  2050.     int i;
  2051.     Unit *unit1 = ((UnitVectorEntry *) e1)->unit;
  2052.     Unit *unit2 = ((UnitVectorEntry *) e2)->unit;
  2053.     
  2054.     if (unit1 == unit2)
  2055.       return 0;
  2056.     if (unit1 == NULL)
  2057.       return 1;
  2058.     if (unit2 == NULL)
  2059.       return -1;
  2060.     for (i = 0; i < MAXSORTKEYS; ++i) {
  2061.     switch (tmpsortkeys[i]) {
  2062.       case byside:
  2063.         if (unit1->side != unit2->side) {
  2064.         int s1 = side_number(unit1->side);
  2065.         int s2 = side_number(unit2->side);
  2066.         
  2067.         /* Put independents at the end of any list. */
  2068.         if (s1 == 0)
  2069.           s1 = numsides + 1;
  2070.         if (s2 == 0)
  2071.           s2 = numsides + 1;
  2072.         return (s1 - s2);
  2073.         }
  2074.         break;
  2075.       case bytype:
  2076.         if (unit1->type != unit2->type) {
  2077.         return (unit1->type - unit2->type);
  2078.         }
  2079.         break;
  2080.       case byname:
  2081.         if (unit1->name) {
  2082.         if (unit2->name) {
  2083.             return strcmp(unit1->name, unit2->name);
  2084.         } else {
  2085.             return -1;
  2086.         }
  2087.         } else if (unit1->number > 0) {
  2088.         if (unit2->name) {
  2089.             return 1;
  2090.         } else if (unit2->number > 0) {
  2091.             return (unit1->number - unit2->number);
  2092.         } else {
  2093.             return -1;
  2094.         }
  2095.         } else if (unit2->name) {
  2096.         return 1;
  2097.         } else if (unit2->number > 0) {
  2098.         return 1;
  2099.         }
  2100.         break;
  2101.       case byactorder:
  2102.         /* (should sort by action priority?) */
  2103.         break;
  2104.       case bylocation:
  2105.         if (unit1->y != unit2->y) {
  2106.         return (unit2->y - unit1->y);
  2107.         } else if (unit1->x != unit2->x) {
  2108.         return (unit1->x - unit2->x);
  2109.         } else {
  2110.         /* Both units are at the same location. Sort by transport. */
  2111.         if (unit1->transport) {
  2112.             if (unit2->transport) {
  2113.             } else {
  2114.             return 1;
  2115.             }
  2116.         } else {
  2117.             if (unit2->transport) {
  2118.             return -1;
  2119.             } else {
  2120.             }
  2121.         }
  2122.         }
  2123.         break;
  2124.       case bynothing:
  2125.         return (unit1->id - unit2->id);
  2126.       default:
  2127.         break;
  2128.     }
  2129.     }
  2130.     /* Unit ids are all unique, so this is a reliable default sort key. */
  2131.     return (unit1->id - unit2->id);
  2132. }
  2133.  
  2134. void
  2135. sort_unit_vector(vec)
  2136. UnitVector *vec;
  2137. {
  2138.     qsort(vec->units, vec->numunits, sizeof(UnitVectorEntry),
  2139.       compare_units_by_keys);
  2140. }
  2141.  
  2142. Obj *
  2143. get_x_property(unit, subkey)
  2144. Unit *unit;
  2145. int subkey;
  2146. {
  2147.     Obj *lis, *bdg;
  2148.  
  2149.     for_all_list(unit_hook(unit), lis) {
  2150.     bdg = car(lis);
  2151.     if (symbolp(car(bdg)) && keyword_code(c_string(car(bdg))) == subkey)
  2152.       return cdr(bdg);
  2153.     }
  2154.     return lispnil;
  2155. }
  2156.  
  2157. Obj *
  2158. get_x_property_by_name(unit, str)
  2159. Unit *unit;
  2160. char *str;
  2161. {
  2162.     Obj *lis, *bdg;
  2163.  
  2164.     for_all_list(unit_hook(unit), lis) {
  2165.     bdg = car(lis);
  2166.     if (symbolp(car(bdg)) && strcmp(c_string(car(bdg)), str) == 0)
  2167.       return cdr(bdg);
  2168.     }
  2169.     return lispnil;
  2170. }
  2171.  
  2172. #ifdef DESIGNERS
  2173.  
  2174. /* A designer can call this to create an arbitrary unit during the game. */
  2175.  
  2176. Unit *
  2177. designer_create_unit(side, u, s, x, y)
  2178. Side *side;
  2179. int u, s, x, y;
  2180. {
  2181.     Unit *newunit;
  2182.     Side *side2;
  2183.  
  2184.     if (!type_can_occupy_cell(u, x, y))
  2185.       return NULL;
  2186.     newunit = create_unit(u, TRUE);
  2187.     if (newunit != NULL) {
  2188.     if (s != 0) {
  2189.         side2 = side_n(s);
  2190.         if (unit_allowed_on_side(newunit, side2)) {
  2191.             set_unit_side(newunit, side2);
  2192.             set_unit_origside(newunit, side2);
  2193.             /* (should ensure that any changed counts are set correctly also) */
  2194.         }
  2195.     }
  2196.     init_supply(newunit);
  2197.     if (can_occupy_cell(newunit, x, y)) {
  2198.         enter_cell(newunit, x, y);
  2199.     } else {
  2200.         /* what? */
  2201.     }
  2202.     see_exact(side, x, y);
  2203.     update_cell_display(side, x, y, TRUE);
  2204.     update_unit_display(side, newunit, TRUE);
  2205.     return newunit;
  2206.     } else {
  2207.     return NULL;
  2208.     }
  2209. }
  2210.  
  2211. /* Move a unit to a given location instantly, with all sides observing.
  2212.    This is for use by designers only! */
  2213.  
  2214. int
  2215. designer_teleport(unit, x, y, other)
  2216. Unit *unit, *other;
  2217. int x, y;
  2218. {
  2219.     if (other != NULL && can_occupy(unit, other)) {
  2220.     leave_cell(unit);
  2221.     enter_transport(unit, other);
  2222.     all_see_cell(x, y);
  2223.     return TRUE;
  2224.     } else if (can_occupy_cell(unit, x, y)) {
  2225.     leave_cell(unit);
  2226.     enter_cell(unit, x, y);
  2227.     all_see_cell(x, y);
  2228.     return TRUE;
  2229.     } else {
  2230.     return FALSE;
  2231.     }
  2232. }
  2233.  
  2234. int
  2235. designer_change_side(unit, side)
  2236. Unit *unit;
  2237. Side *side;
  2238. {
  2239.     Side *side2;
  2240.  
  2241.     change_unit_side(unit, side, -1, NULL);
  2242.     for_all_sides(side2) {
  2243.     if (1 /* side2 should see change */) {
  2244.         update_unit_display(side2, unit, TRUE);
  2245.     }
  2246.     }
  2247.     return TRUE;
  2248. }
  2249.  
  2250. int
  2251. designer_disband(unit)
  2252. Unit *unit;
  2253. {
  2254.     kill_unit(unit, -1);
  2255.     return TRUE;
  2256. }
  2257.  
  2258. #endif /* DESIGNERS */
  2259.  
  2260.